﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ReportsViewModel.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Reports map view model
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.ViewModels
{
    #region Using Directives

    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Threading;
    using System.Windows.Browser;
    using System.Windows.Controls;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;
    using Messages;
    using Microsoft.Maps.MapControl;
    using StreetCareAPI;

    #endregion

    /// <summary>
    /// Reports map view model
    /// </summary>
    public class ReportsViewModel : BaseViewModel
    {
        #region Private members

        /// <summary>
        /// Private store for reports
        /// </summary>
        private ObservableCollection<Report> reports;

        /// <summary>
        /// Private store for customer
        /// </summary>
        private Customer customer;

        /// <summary>
        /// Private store fot the BubbleVisible property
        /// </summary>
        private bool bubbleVisible;

        #endregion

        #region Class constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="ReportsViewModel"/> class.
        /// </summary>
        public ReportsViewModel()
        {
            if (HtmlPage.IsEnabled)
            {
                Messenger.Default.Register<Customer>(this, (customer => this.Customer = customer));
                Messenger.Default.Register<ObservableCollection<Report>>(this, (reports => this.Reports = reports));
                Messenger.Default.Register<NewReportCompletedMessage>(this, this.NewReportCompleted);
                Messenger.Default.Register<CloseBubbleMessage>(this, (message => this.ReportUnselected()));
            }
        }

        #endregion

        #region ViewModel Properties

        /// <summary>
        /// Gets or sets the customer.
        /// </summary>
        /// <value>
        /// The customer.
        /// </value>
        public Customer Customer
        {
            get
            {
                return this.customer;
            }

            set
            {
                this.customer = value;
                this.ViewMap.SetView(value.CustomerMapLocation, value.CustomerZoom);
                this.RaisePropertyChanged("Customer");
            }
        }

        /// <summary>
        /// Gets the culture.
        /// </summary>
        public CultureInfo Culture
        {
            get
            {
                return Thread.CurrentThread.CurrentCulture;
            }
        }

        /// <summary>
        /// Gets or sets the reports collection.
        /// </summary>
        /// <value>The reports.</value>
        public ObservableCollection<Report> Reports
        {
            get
            {
                return this.reports;
            }

            set
            {
                this.reports = value;
                RaisePropertyChanged("Reports");
            }
        }

        /// <summary>
        /// Gets or sets the view map.
        /// </summary>
        /// <value>The view map.</value>
        public Map ViewMap { get; set; }

        /// <summary>
        /// Gets or sets the report bubble.
        /// </summary>
        /// <value>
        /// The report bubble.
        /// </value>
        public Canvas ReportBubble { get; set; }

        #endregion

        #region Commands

        /// <summary>
        /// Gets the select report command reference.
        /// </summary>
        /// <value>The select report command.</value>
        public ICommand SelectReportCommand
        {
            get
            {
                return new RelayCommand<Report>(this.ReportSelected);
            }
        }

        /// <summary>
        /// Gets the unselect report command reference.
        /// </summary>
        /// <value>The unselect report command.</value>
        public ICommand UnselectReportCommand
        {
            get
            {
                return new RelayCommand(this.ReportUnselected);
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [bubble visible].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [bubble visible]; otherwise, <c>false</c>.
        /// </value>
        public bool BubbleVisible
        {
            get
            {
                return this.bubbleVisible;
            }

            set
            {
                this.bubbleVisible = value;
                this.RaisePropertyChanged("BubbleVisible");
            }
        }

        /// <summary>
        /// Handles the selected report command.
        /// </summary>
        /// <param name="report">The report</param>
        public void ReportSelected(Report report)
        {
            // TODO: Try to do this with databinding
            MapLayer.SetPosition(this.ReportBubble, report.ReportMapLocation);

            this.BubbleVisible = true;
            Messenger.Default.Send(new SelectReportMessage { Report = report });
        }

        /// <summary>
        /// Handles the unselected report command.
        /// </summary>
        public void ReportUnselected()
        {
            this.BubbleVisible = false;
        }

        #endregion

        #region Event Methods

        /// <summary>
        /// Handles the new report completed event.
        /// </summary>
        /// <param name="message">The message.</param>
        public void NewReportCompleted(NewReportCompletedMessage message)
        {
            if (message.IsError)
            {
                return;
            }

            var report = message.Report;
            this.Reports.Add(report);
            this.ViewMap.Center = report.ReportMapLocation;
        }

        #endregion
    }
}
